package com.pet.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.pet.system.controller.vo.UserVo;
import com.pet.system.entity.User;
import com.pet.system.mapper.UserMapper;
import com.pet.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getInfo(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public PageInfo<User> getPageList(UserVo user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StringUtil.isNotEmpty(user.getRole()),User::getRole,user.getRole())
                .eq(null!=user.getSex(),User::getSex,user.getSex())
                .like(StringUtil.isNotEmpty(user.getPhone()),User::getPhone,user.getPhone())
                .like(StringUtil.isNotEmpty(user.getUsername()),User::getUsername,user.getUsername())
                .like(StringUtil.isNotEmpty(user.getNickname()),User::getNickname,user.getNickname())
        ;

        return PageHelper.startPage(user.getPageNo(), user.getPageSize())
                .doSelectPageInfo(() -> userMapper.selectList(queryWrapper));
    }

    @Override
    public int remove(Long[] ids) {
        for (Long id : ids) {
            int i = userMapper.deleteById(id);
        }
        return 1;
    }

    @Override
    public int edit(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int add(User user) {
        user.setCreateTime(new Date());
        return userMapper.insert(user);
    }
}
